﻿#include "mainwindow.h"
#include <QApplication>
#include <QStandardPaths>
#include <QSplashScreen>


std::shared_ptr<spdlog::logger> logger  = spdlog::daily_logger_mt("daily_logger", "logs/daily.txt", 0, 0);




// 编译器链接Windows调试帮助库
#pragma comment(lib, "Dbghelp.lib")
#include <WinSock2.h>
#include <DbgHelp.h>
#include <windows.h>
#include <winnt.h>
// 创建dump文件的函数
void CreateMiniDump(EXCEPTION_POINTERS* pep, char* strDumpFile)
{
    HANDLE hFile = CreateFileA(strDumpFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
    if (hFile!= NULL && hFile!= INVALID_HANDLE_VALUE)
    {
        MINIDUMP_EXCEPTION_INFORMATION mdei;
        mdei.ThreadId = GetCurrentThreadId();
        mdei.ExceptionPointers = pep;
        mdei.ClientPointers = TRUE;
        MINIDUMP_TYPE mdt = MiniDumpWithDataSegs;
        BOOL rv = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, &mdei, 0, 0);
        if (rv)
        {
            // 可添加日志记录创建成功
        }
        else
        {
            // 可添加日志记录创建失败及错误码
        }
        CloseHandle(hFile);
    }
}

// 异常处理函数
LONG WINAPI MyCustomFilter(EXCEPTION_POINTERS* pep)
{
    QString strDumpFile;
    strDumpFile.sprintf("Dump_%s_%s.dmp", QDate::currentDate().toString("yyyyMMdd").toStdString().c_str(), QTime::currentTime().toString("HHmmss").toStdString().c_str());
    CreateMiniDump(pep, strDumpFile.toUtf8().data());
    return EXCEPTION_EXECUTE_HANDLER;
}

#include <fstream>
#include <vector>
#include <sstream>
#include <tlhelp32.h>
std::string UnicodeToAscii(const std::wstring& in_str)
{
    int nNeedChars = WideCharToMultiByte(CP_ACP, 0, in_str.c_str(), -1, 0, 0, 0, 0);
    if (nNeedChars > 0)//再次判断一下
    {
        std::vector<char> temp(nNeedChars);
        ::WideCharToMultiByte(CP_ACP, 0, in_str.c_str(), -1, &temp[0], nNeedChars, 0, 0);
        return std::string(&temp[0]);
    }

    return std::string();
}
bool isProcessRunning(const std::string& processName) {
    HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    PROCESSENTRY32 pe;
    pe.dwSize = sizeof(PROCESSENTRY32);

    if (!Process32First(hSnapShot, &pe))
    {
        return false;
    }

    int number = 0;
    while (Process32Next(hSnapShot, &pe))
    {
        wstring strTemp = pe.szExeFile;
        string strProcessTemp = UnicodeToAscii(strTemp);
        if (processName == strProcessTemp)
        {
            number++;
        }
    }
    if (number > 1)
    {
        return true;
    }
    return false;
}

#include <chrono>
using namespace std;
using namespace std::chrono;

int main(int argc, char *argv[])
{






    QApplication a(argc, argv);

//    // 系统当前时间
//        system_clock::time_point today = system_clock::now();
//        // 系统当前时间
//        time_t now = system_clock::to_time_t(today);

//        if (now > tm2)
//        {
//            return false;
//        }


    string path = (QCoreApplication::applicationDirPath() + "/password").toLocal8Bit().toStdString();

    int64_t value = verify(path.c_str());

    if (value ==0)
    {
        QMessageBox::warning(nullptr, "", "没有安装加密文件");
        return 1;
    }



    time_t tm = (time_t)(value);


    // 获取当前时间
    time_t now = time(NULL);


    // 计算时间差（秒）
    double diff_seconds = difftime(tm,now);

    double diff_day = diff_seconds/(24*60*60);

    if (diff_day <0 ) {
        QMessageBox::warning(nullptr, "", "软件过期");
        return 1;
    }

    if (diff_day <30 ) {
        QMessageBox::warning(nullptr,"", "软件有效时间小于30天" );
    }

//    bool state = isProcessRunning("gl.exe");
//    if (state)
//    {
//        QMessageBox::warning(nullptr, "", "程序已经运行");
//        return 1;

//    }


    // 设置异常处理函数
    SetUnhandledExceptionFilter(MyCustomFilter);


    QFile styleFile(QCoreApplication::applicationDirPath() + "/para/dark.qss");
    if(styleFile.open(QFile::ReadOnly))
    {
        qApp->setStyleSheet(styleFile.readAll());
        styleFile.close();
    }

    QString m_para_dir = QCoreApplication::applicationDirPath() + "/para";
    QString path_ini;
    path_ini = m_para_dir + "/para.ini";
    //创建配置文件操作对象
    QSettings* m_settings_para = new QSettings(path_ini, QSettings::IniFormat);
    m_settings_para->setIniCodec("UTF-8");
    int font_size = m_settings_para->value("para/font_size", 12).toInt();
    delete m_settings_para;

    QFont main_font("Microsoft YaHei", font_size);

    a.setFont(main_font);



    QString path_pixmap   = QCoreApplication::applicationDirPath() + "/picture/background.svg";
    QPixmap pixmap(path_pixmap);
    QSplashScreen *splash = new QSplashScreen;

    QFont font("Microsoft YaHei",50);


    splash->setFont(font);
    splash->showMessage("程序启动中...", Qt::AlignCenter, Qt::white);	//显示文字、对齐方式、文字颜色
    splash->setPixmap(pixmap);										//绑定图片
    splash->show();                            						//显示启动画面
    logger->flush_on(spdlog::level::info);


    MainWindow w;

    w.show();
    splash->finish(&w);											//关闭启动界面
    delete splash;



    return a.exec();


}
